package com.pengtu.gsj.service.impl.check.gcjs;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.gcjs.YHRoadConstructionADao;
import com.pengtu.gsj.dao.check.gcjs.YHRoadConstructionBDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.gcjs.YHRoadConstructionB;
import com.pengtu.gsj.entity.gcjs.YHRoadConstructionA;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.gcjs.RoadHardenConService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class RoadHardenConServiceImpl implements RoadHardenConService{

	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private YHRoadConstructionADao yhRoadConstructionADao;
	@Autowired
	private YHRoadConstructionBDao yhRoadConstructionBDao;
	
	@Override
	public List<YHRoadConstructionB> getRoadHardenConBpros(QueryFilter queryFilter) {
		return yhRoadConstructionBDao.find(queryFilter, "year", true);
	}
	
	@Override
	public Map<String, Object> getRoadHardenCons(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Double> scoreResult = new ArrayList<Double>();
		List<YHRoadConstructionB> yhRoadLists = new ArrayList<YHRoadConstructionB>();
		List<YHRoadConstructionA> yhRoadConstrAs = new ArrayList<YHRoadConstructionA>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		//检索条件
		String projectNameId = (String)jsonObject.getString("itemProjectId");
		//单项满分
		double totalScore = Double.parseDouble((String)jsonObject.get("yearFull"));
		//每个未开工的项目扣分
		double notWork = Double.parseDouble((String)jsonObject.get("firstStepScore"));
		//每个开工未完工的项目扣分
		double notComp = Double.parseDouble((String)jsonObject.get("secondStepScore"));
		//每个完工未验收的项目扣分
		double notAccep = Double.parseDouble((String)jsonObject.get("thirdStepScore"));
		//每个新农村公路路面硬化工程项目满分
		double hardRoadScore = Double.parseDouble((String)jsonObject.get("fourthStepScore"));
		//每项工程发现一个问题扣分
		double reduScore = Double.parseDouble((String)jsonObject.get("fifthStepScore"));
		double total =0; 		   //检查得分
		double totalOne =0; 	//一表检查得分
		double totalTwo =0; 	//二表检查得分
		double percentage =0;    //系统完善度
		double itemNum = 0; //工程总数
		double workNum = 0; //开工总数
		double completeNum = 0; //完工总数
		double acceptNum = 0; //验收总数
		double tableFull = totalScore + hardRoadScore * 2;// 每个部、省补助新农村公路路面硬化工程项目总分
		
		//根据projectId和时间、标签查询是否有附件
		list = systemManageService.getCheckAttachmentsByPartId(startTime, endTime, tag, projectNameId);
		//表一的算分
		yhRoadConstrAs = yhRoadConstructionADao.getCheckDataAByYear(startTime, endTime);
		if (yhRoadConstrAs.size() > 0){
			for (YHRoadConstructionA yhConstrA : yhRoadConstrAs) {
				//获得项目总数、开工总数、完工总数、验收总数
				itemNum += yhConstrA.getItemNum();
				workNum += yhConstrA.getWorkNum();
				completeNum += yhConstrA.getCompleteNum();
				acceptNum += yhConstrA.getAcceptNum();
			}
			map.put("flagOne", true);
		}
		
		if (workNum > 0 && Math.abs(workNum - completeNum) == 0 && Math.abs(completeNum - acceptNum) == 0) {
			totalOne = totalScore;
		} else {
			//表格总分减去扣分的情况
			totalOne = MathsUtils.sub(totalScore, (itemNum - workNum) * notWork, (workNum - completeNum) * notComp, (completeNum - acceptNum) * notAccep);
		}
		totalOne = MathsUtils.formatDoubleUp(totalOne, 1);
		totalOne = totalOne > 0 ? totalOne : 0;
		map.put("scoreFirst", totalOne);
		values.add("true");
	
			
		//表二的评分
		if (StringUtils.isNotEmpty(projectNameId)) {
			//当检索条件不为空的时候更新分数
			values.add(projectNameId);
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = projectNameId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				//根据tag检索是否有附件
				if( i > 1) {
					break;
				}
			YHRoadConstructionB yhRoadB = yhRoadConstructionBDao.get(idArrs[i]);
			yhRoadLists.add(yhRoadB);
			//根据文件管理的所选择的情况进行评分，其中计划规模根据符合程度有四种状态可选
			if ("1".equals(yhRoadB.getWorkSituation())) {
				totalTwo =  hardRoadScore - reduScore;
			} 
			if ("3".equals(yhRoadB.getPlanScale())) {
				totalTwo = hardRoadScore - 3 * reduScore;
			} else if ("2".equals(yhRoadB.getPlanScale())) {
				totalTwo = hardRoadScore - 2 * reduScore;
			} else if ("1".equals(yhRoadB.getPlanScale())) {
				totalTwo = hardRoadScore - reduScore;
			} 
			if ("1".equals(yhRoadB.getCompleteSituation())) {
				totalTwo = hardRoadScore - reduScore;
			}
			totalTwo = MathsUtils.formatDoubleUp(totalTwo, 1);
			total += totalTwo;
			scoreResult.add(totalTwo);
			map.put("flagTwo", true);
			}
		}
		//计算总得分
		total = MathsUtils.formatDouble(total + totalOne, 1);
		percentage = MathsUtils.divDouble(MathsUtils.mulDouble(total, 100), tableFull, 2);
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("itemNum", itemNum);
		map.put("workNum", workNum);
		map.put("completeNum", completeNum);
		map.put("acceptNum", acceptNum);
		map.put("percent", percentage);
		map.put("checkTable", checkTable);
		map.put("yhRoadConstrAs", yhRoadConstrAs);
		map.put("yhRoadLists", yhRoadLists);
		map.put("scoreResult", scoreResult);
		map.put("score", checkTable.getTestFull());
		return map;
	}

	/**
	 * 将检索条件封装，进行YHRoadConstructionB的文件查询
	 */
	@Override
	public List<YHRoadConstructionB> getCheckDataB(String startTime, String endTime, String id) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("year", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("year",  endTime, MatchType.LE);
		}
		if (StringUtils.isNotEmpty(id)) {
			queryFilter.addFilter("id", id, MatchType.EQ);
		}
		return yhRoadConstructionBDao.find(queryFilter);
	}
}
